import request from "@/utils/request";
import router from "@/router";
import { Local } from "@/utils/storage";
import { Notify } from "vant";
import { useRoute } from "vue-router";
import NavBarConfig from '@/router/navbar.config'
const route = useRoute();
// 模拟登陆，实际需要调用request进行登录请求
export function login(userInfo) {
    //这个存储主要是为了再登陆
    Local.set('loginInfo', userInfo);
    request.post('user/login', userInfo).then(res => {

        if (res.code === 2002) {
            Notify({ type: 'success', message: res.message });
            Local.set('userInfo', res.data);
            const userInfo = Local.get('userInfo')
            if(userInfo.role === 1|| userInfo.role === 0){
                const addRouteIndex = NavBarConfig.findIndex(route => route.name === 'add');
                if (addRouteIndex !== -1) {
                    // Update requiresAuth to true for the 'add' route
                    NavBarConfig[addRouteIndex].meta.requiresAuth = true;
                }
            }else{
                const addRouteIndex = NavBarConfig.findIndex(route => route.name === 'add');
                if (addRouteIndex !== -1) {
                    // Update requiresAuth to true for the 'add' route
                    NavBarConfig[addRouteIndex].meta.requiresAuth = false;
                }
            }
            router.push({ path: '/' });
        } else {
            Notify({ type: 'danger', message: res.message });
        }
    }).catch(error => {
        return error;
    });
}
export function checkLogin() {
    const loginInfo = Local.get('loginInfo');
    request.post('user/login', loginInfo).then(res => {
        if (res.code === 2002) {
            Notify({ type: 'success', message: res.message });
            Local.set('userInfo', res.data);
            const userInfo = Local.get('userInfo')
            if(userInfo.role === 1 || userInfo.role === 0){
                const addRouteIndex = NavBarConfig.findIndex(route => route.name === 'add');
                if (addRouteIndex !== -1) {
                    // Update requiresAuth to true for the 'add' route
                    NavBarConfig[addRouteIndex].meta.requiresAuth = true;
                }
            }else{
                const addRouteIndex = NavBarConfig.findIndex(route => route.name === 'add');
                if (addRouteIndex !== -1) {
                    // Update requiresAuth to true for the 'add' route
                    NavBarConfig[addRouteIndex].meta.requiresAuth = false;
                }
            }
            router.push({
                path: route.fullPath,
                query: {
                    refresh: new Date().getTime() // 使用时间戳来避免路由缓存
                }
            });
        } else {
            Notify({ type: 'danger', message: res.message });
        }
    }).catch(error => {
        return error;
    });
}

export function logout() {
    Local.clear();
    request.get('user/logout').then(res => {
        if (res.code === 2003) {
            Notify({ type: 'success', message: res.message });
        }
        else {
            Notify({ type: 'danger', message: res.message });
        }
    }).catch(error => {
        return error;
    });
    router.push({ path: '/login' });
}
export function register(registerInfo, repassword) {
    console.log(registerInfo);
    if (registerInfo.password !== repassword) {
        Notify({ type: 'danger', message: '两次密码不一致' });
        return;
    }
    request.post('user/register', registerInfo).then(res => {
        if (res.code === 2001) {
            window.location.href = '/';
            Notify({ type: 'success', message: res.message });
            return res.data;
        }
        else {
            Notify({ type: 'danger', message: res.data });
            return ''
        }
    }).catch(error => {
        return error;
    });
}

export async function uploadPic(file) {
    const forms = new FormData();
    forms.append("file", file.file); // 获取上传图片信息

    try {
        const res = await request.post('user/upload-pic', forms, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        });

        if (res.code == 2020) {
            Notify({ type: 'success', message: res.message });
            return res.data;
        } else {
            Notify({ type: 'danger', message: res.message });
            return null
        }

    } catch (error) {
        return error;
    }
}
export function setNewPassword(forgetForm, repassword) {
    if (forgetForm.password !== repassword) {
        Notify({ type: 'danger', message: '两次密码不一致' });
        return;
    }
    request.post('user/change-password', forgetForm).then(res => {
        if (res.code === 2000) {
            Notify({ type: 'success', message: res.message });
            window.location.href = '/';
        }
        else {
            Notify({ type: 'danger', message: res.data });
        }
    }).catch(error => {
        return error;
    });


}
export function getCode(phone) {
    request.get('user/get-code', {
        params: {
            phone: phone
        }
    }).then(res => {
        if (res.code === 2004) {
            Notify({ type: 'success', message: res.message });
        }
        else {
            Notify({ type: 'danger', message: res.message });
        }
    }).catch(error => {
        return error;
    });

    console.log(phone);
}
export function codeLogin(form) {
    request.get('user/code-login', {
        params: {
            phone: form.phone,
            code: form.code
        }
    }
    ).then(res => {
        if (res.code === 2002) {
            Local.set('userInfo', res.data);
            window.location.href = '/';
            Notify({ type: 'success', message: res.message });
        }
        else {
            Notify({ type: 'danger', message: res.message });
        }
    }).catch(error => {
        return error;
    });

}